//
//  HWVipMedalCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2021/12/16.
//  Copyright © 2021 94Wan. All rights reserved.
//

import UIKit

class HWVipMedalCell: UITableViewCell {
    var bgView:UIView!
    var tagImageView:UIButton!
    var headImageView:UIImageView!
    
    var nameLabel:UILabel!
    var vipTagImageView:UIImageView!
    var contentLabel:UILabel!
    var receiveButton:UIButton!
    var vipTips:UILabel!
//    var borderColor = UIColor.colorWithHexString(hexString: "#E7BD60")
    var receiveButtonClick:((_:String)->())?
    var model:HWHeadPortraitFrameModel? {
        didSet {

            nameLabel.text = model?.pet_name
            contentLabel.text = model?.note
            vipTips.text = model?.title
            vipTagImageView.sd_setImage(with: URL(string: model?.medal_img ?? ""), completed: nil)
            
            headImageView.sd_setImage(with: URL(string: model?.head_img ?? ""), completed: nil)
            tagImageView.setTitle(model?.name, for: .normal)
            
            if model?.is_peidai == 0 {
                receiveButton.setTitle("穿戴", for: .normal)
                receiveButton.setTitleColor(UIColor.colorWithHexString(hexString: "#4F3200"), for: .normal)
                receiveButton.setBackgroundImage(UIImage(named: "wearButton"), for: .normal)
//                borderColor = .colorWithHexString(hexString: "#F3F4F8")
                bgView.layer.shadowColor = UIColor.clear.cgColor
                bgView.layer.borderColor = UIColor.clear.cgColor
            }else if model?.is_peidai == 1 {
                receiveButton.setTitle("脱下", for: .normal)
                receiveButton.setTitleColor(UIColor.colorWithHexString(hexString: "#4F3200"), for: .normal)
                receiveButton.setBackgroundImage(UIImage(named: "takeOffButton"), for: .normal)
                bgView.layer.shadowColor = UIColor.colorWithHexString(hexString: "#E8EAF1").cgColor
                bgView.layer.shadowOffset = CGSize(width:0,height:2)
                bgView.layer.shadowOpacity = 1
                bgView.layer.shadowRadius = 4
                bgView.layer.borderColor = UIColor.colorWithHexString(hexString: "#E7BD60").cgColor
                bgView.layer.borderWidth = 1
//                borderColor = .colorWithHexString(hexString: "#E7BD60")
                
            }else if model?.is_peidai == 2 {
                receiveButton.setTitle("领取", for: .normal)
                receiveButton.setTitleColor(UIColor.colorWithHexString(hexString: "#666666"), for: .normal)
                receiveButton.setBackgroundImage(UIImage(named: "receiveButtonGray"), for: .normal)
//                borderColor = .colorWithHexString(hexString: "#F3F4F8")
                bgView.layer.shadowColor = UIColor.clear.cgColor
                bgView.layer.borderColor = UIColor.clear.cgColor
            }
//            bgView.layer.borderColor = borderColor.cgColor
        }
    }
    
    class func cellWithTableView(tableView:UITableView) -> (HWVipMedalCell) {
        let reUse:String = "HWVipMedalCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWVipMedalCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWVipMedalCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
//        bgView.addCornerAndBorder(conrners: [.topRight,.bottomLeft,.bottomRight], radius: 16*widthScale, borderWidth: 1, borderColor: borderColor)
    }

    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        bgView = UIView()
        bgView.backgroundColor = .colorWithHexString(hexString: "#F3F4F8")
        bgView.layer.cornerRadius = 13
        bgView.layer.borderWidth = 1
        contentView.addSubview(bgView)
        
        tagImageView = UIButton(type: .custom)
        tagImageView.setBackgroundImage(UIImage(named: "medalLevelBg"), for: .normal)
        tagImageView.titleLabel?.font = .boldSystemFont(ofSize: 10)
        contentView.addSubview(tagImageView)
        
        headImageView = UIImageView()
        headImageView.backgroundColor = .clear
        headImageView.layer.cornerRadius = 22*widthScale
        headImageView.layer.masksToBounds = true
        bgView.addSubview(headImageView)
        
        nameLabel = HWLabel.createBoldLabel(title: "", color: "#00B7EA", font: 12*widthScale)
        bgView.addSubview(nameLabel)
        
        vipTagImageView = UIImageView()
        bgView.addSubview(vipTagImageView)
        
        contentLabel = HWLabel.createLabel(title: "", color: "#666666", font: 10*widthScale)
        bgView.addSubview(contentLabel)
        
        receiveButton = UIButton(type: .custom)
        receiveButton.setTitle("穿戴", for: .normal)
        receiveButton.titleLabel?.font = .boldSystemFont(ofSize: 12*widthScale)
        receiveButton.setTitleColor(.colorWithHexString(hexString: "#4F3200"), for: .normal)
        receiveButton.setBackgroundImage(UIImage(named: "wearButton"), for: .normal)
        receiveButton.addTarget(self, action: #selector(receiveButtonClick(_:)), for: .touchUpInside)
        bgView.addSubview(receiveButton)
        
        vipTips = HWLabel.createLabel(title: "", color: "#999999", font: 10*widthScale)
        bgView.addSubview(vipTips)
    }
    
    func initFrame() -> Void {
        bgView.snp.makeConstraints { make in
            make.top.equalTo(28*widthScale)
            make.left.equalTo(15*widthScale)
            make.right.equalTo(-15*widthScale)
            make.bottom.equalToSuperview()
        }
        
        tagImageView.snp.makeConstraints { make in
            make.left.equalTo(15*widthScale)
            make.bottom.equalTo(bgView.snp.top).offset(8*widthScale)
            make.width.equalTo(79*widthScale)
            make.height.equalTo(18*widthScale)
        }
        
        headImageView.snp.makeConstraints { make in
            make.left.equalTo(16*widthScale)
            make.top.equalTo(16*widthScale)
            make.bottom.equalTo(-13*widthScale)
            
            make.width.height.equalTo(44*widthScale)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(headImageView.snp.right).offset(14*widthScale)
            make.top.equalTo(18*widthScale)
        }
        
        vipTagImageView.snp.makeConstraints { make in
            make.centerY.equalTo(nameLabel)
            make.left.equalTo(nameLabel.snp.right).offset(4*widthScale)
            make.width.equalTo(20*widthScale)
            make.height.equalTo(20*widthScale)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(5*widthScale)
        }
        
        receiveButton.snp.makeConstraints { make in
            make.right.equalTo(-14*widthScale)
            make.top.equalTo(13*widthScale)
            make.width.equalTo(56*widthScale)
            make.height.equalTo(27*widthScale)
        }
        
        vipTips.snp.makeConstraints { make in
            make.top.equalTo(receiveButton.snp.bottom).offset(5*widthScale)
            make.centerX.equalTo(receiveButton)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
extension HWVipMedalCell {
    @objc func receiveButtonClick(_ button:UIButton) -> Void {
        self.receiveButtonClick?(self.model?.id ?? "")
    }
}
